package com.jinlejia.bbs.logic.mine;

import android.content.Context;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.http.entity.MineEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.*;

/**
 * @author 王进 on 2019/3/25.
 */
//个人中心
public class MineLogic {
    private final LogicCallBack callBack;
    private final Context context;

    public MineLogic(Fragment fragment, LogicCallBack callBack) {
        this.context = fragment.getContext();
        this.callBack = callBack;
    }

    public void execute() {
        JSONObject json = new JSONObject();
        httpCallForm(BBSConfig.minePage,json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onNoResponse(int code) {
                callBack.onResult(null);
            }
            @Override
            public void onError(Exception e) {
                callBack.onResult(null);
            }

            @Override
            public void onResponse(JsonParse parse) {
                MineEntity entity = parse.parse(MineEntity.class);
                BBSUserBean user = new BBSUserBean();
                user.setUserName(entity.getUserName());
                user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                if (!TextUtils.isEmpty(entity.getUserDeptName())){
                    user.setUserAddress(entity.getUserDeptName());
                }else {
                    user.setUserAddress(entity.getDuty());
                }
                user.setAttitudesCount(entity.getAttitudes());  //获赞数量
                user.setAttentionCount(entity.getAttention()); //关注数量
                user.setCollectCount(entity.getCollection()); //收藏数量
                user.setAttentionedCount(entity.getAttentioned());  //被关注数量-粉丝数量
                callBack.onResult(user);
            }
        }));
    }

    public interface LogicCallBack {
        void onResult(BBSUserBean userBean);
    }
}
